package com.Alan.dao;

import org.junit.Test;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @CreateDate 2021/8/9 2:40
 */
public class BaseDao {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    static {
        Properties properties = new Properties();
            InputStream ras = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(ras);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取静态资源出错");
        }
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        user = properties.getProperty("user");
        password = properties.getProperty("password");
    }

//    获取数据库连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库连接出错");
        }
        return connection;
    }

//    编写R的公共类
    public static ResultSet execute(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet, String sql, Object[] params) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i + 1, params[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

//    编写CUD的公共类
    public static int execute(Connection connection, PreparedStatement preparedStatement, String sql, Object[] params) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i + 1, params[i]);
        }
        return preparedStatement.executeUpdate();
    }

    public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        boolean flag = true;

        if (connection !=null){
            try {
                connection.close();
                System.gc();
            } catch (Exception throwables) {
                throwables.printStackTrace();
                System.out.println("Connection关闭异常");
                flag = false;
            }
        }

        if (preparedStatement != null){
            try {
                preparedStatement.close();
                System.gc();
            } catch (Exception throwables) {
                throwables.printStackTrace();
                System.out.println("PreparedStatement关闭异常");
                flag = false;
            }
        }

        if (resultSet != null){
            try {
                resultSet.close();
                System.gc();
            } catch (Exception throwables) {
                throwables.printStackTrace();
                System.out.println("ResultSet关闭异常");
                flag = false;
            }
        }
        return flag;
    }
    @Test
    public void test(){
        System.out.println("你好");
    }
}
